package cn.krui.project.controller.simpleshopsetting;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RestController;
import cn.krui.helper.bean.AppResult;
import cn.krui.helper.bean.DataArray;
import cn.krui.project.controller.simpleshopsettingBaseController;
import cn.krui.project.bean.simpleshopsetting.feedback;
import cn.krui.project.bean.simpleshopsetting.feedback_view;
import cn.krui.project.service.simpleshopsetting.feedbackService;
import java.util.List;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.ObjectError;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import cn.krui.helper.tools.DateTimeUtils;
/**
 * createor: zc
 * email: zc8023zs@163.com
 * create_time: 2020/4/8 16:38:01
 */
@Api(tags = "feedback", description = "反馈")
@RestController
@RequestMapping("/simpleshopsetting/feedback")
public class feedbackController extends simpleshopsettingBaseController {

    @Autowired
    private feedbackService _feedbackService;

    @PostMapping(value = "/add")
	@ApiOperation(value = "反馈添加", httpMethod = "POST")
    public AppResult<Integer> add(@Valid feedback model,BindingResult bindingResult){
        AppResult<Integer> app = new AppResult<Integer>();
        if (bindingResult.hasErrors()) {
            List<ObjectError> oerr = bindingResult.getAllErrors();
            for (ObjectError objectError : oerr) {
                DefaultMessageSourceResolvable d = (DefaultMessageSourceResolvable)objectError.getArguments()[0];
                String columnname = d.getDefaultMessage();
                //主键不用判断
                if(columnname.equals("msg_id"))continue;
                app.NO(bindingResult.getAllErrors().get(0).getDefaultMessage());
                return app;
            }
        }
        Integer i = _feedbackService.add(model);
        if(i>0) i = model.getMsg_id();
        app.setDataSingle(i);
        app.OK("添加成功");
        return app;
    }

    @PostMapping(value = "/update")
	@ApiOperation(value = "反馈修改", httpMethod = "POST")
    public AppResult<Integer> update(@Valid feedback model,BindingResult bindingResult){
        AppResult<Integer> app = new AppResult<Integer>();
        if (bindingResult.hasErrors()) {
            List<ObjectError> oerr = bindingResult.getAllErrors();
            for (ObjectError objectError : oerr) {
                DefaultMessageSourceResolvable d = (DefaultMessageSourceResolvable)objectError.getArguments()[0];
                String columnname = d.getDefaultMessage();
                app.NO(bindingResult.getAllErrors().get(0).getDefaultMessage());
                return app;
            }
        }
        Integer i = _feedbackService.update(model);
        app.OK("修改成功");
        app.setDataSingle(i);
        return app;
    }

    @PostMapping(value = "/del")
	@ApiOperation(value = "反馈删除", httpMethod = "POST")
    public AppResult<Integer> del(Integer id){
        AppResult<Integer> app = new AppResult<Integer>();
        Integer i = _feedbackService.del(id);
        app.OK("删除成功");
        app.setDataSingle(i);
        return app;
    }

    @GetMapping(value = "/info")
	@ApiOperation(value = "反馈详情", httpMethod = "GET")
    public AppResult<feedback_view> info(Integer id){
        AppResult<feedback_view> app = new AppResult<feedback_view>();
        feedback_view i = _feedbackService.info(id);
        app.setDataSingle(i);
        app.OK("查询成功");
        return app;
    }

    @PostMapping(value = "/select")
	@ApiOperation(value = "反馈列表", httpMethod = "POST")
    public AppResult<feedback_view> select(
        ){
        AppResult<feedback_view> app = new AppResult<feedback_view>();
        QueryWrapper<feedback_view> where = new QueryWrapper<feedback_view>();
        List<feedback_view> i = _feedbackService.select(where);
        DataArray<feedback_view> da=new DataArray<feedback_view>();
        da.setDataList(i);
        app.setDataArray(da);
        app.OK("查询成功");
        return app;
    }

    @PostMapping(value = "/selectpage")
    @ApiOperation(value = "反馈列表分页", httpMethod = "POST")
    public AppResult<feedback_view> selectpage(
            @ApiParam(value = "页", required = true) @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @ApiParam(value = "条", required = true) @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        AppResult<feedback_view> app = new AppResult<feedback_view>();
        QueryWrapper<feedback_view> where = new QueryWrapper<feedback_view>();
        IPage<feedback_view> pg = new Page<feedback_view>();
        pg.setCurrent(page); 
        pg.setSize(size);
        IPage<feedback_view> i = _feedbackService.select(pg,where);
        int total = _feedbackService.selectCount(where);
        DataArray<feedback_view> da = new DataArray<feedback_view>();
        da.setPageCount((total/size)+1);
        da.setDataCount(total);
        da.setPageIndex(page);
        da.setPageSize(size);
        da.setDataList(i.getRecords());
        app.setDataArray(da);
        app.OK("查询成功");
        return app;
    }

}